import React, { Component } from 'react'
import  {login as UserLogin} from  "../interface/user";

interface  IState{
  username:string,
  userpwd:string
}

export default class Login extends Component<any,IState> {
  constructor(props:any){
    super(props);
    this.state = {
        username:"",
        userpwd:""
    }

  }
  login = ()=>{//登录
    UserLogin({
      username:this.state.username,
      userpwd:this.state.userpwd
    }).then(res=>{
      console.log(res);
      
        if(res.data.code===200){//登录成功
            window.localStorage.setItem("token",res.data.token);
            this.props.history.push("/main");
        }
    })

  }

  render() {
    return (
      <div>
          <h1>登录页面</h1>
          <div>
            用户名  <input value={this.state.username} onChange={(e)=>{
              this.setState({
                username:e.target.value
              })
            }}/>
          </div>
          <div>
            密码  <input value={this.state.userpwd} onChange={(e)=>{
              this.setState({
                userpwd:e.target.value
              })
            }}/>
          </div>
          <button onClick={()=>{
            this.login();
          }}>登录</button>
      </div>
    )
  }
}
